#!/bin/bash

echo "================ 开始运行deploy_pro.sh脚本 ================"
echo "${CI_PROJECT_DIR}"

expect -c "
set timeout -1
spawn ssh ${FZIDT_DC_USER}@${FZIDT_DC_IP} \"docker ps | awk '{print $NF}' | grep ${DEPLOY_CONTAINER_NAME} && docker stop ${DEPLOY_CONTAINER_NAME}\"
expect {
    \"*assword\" {send \"${FZIDT_DC_PASS}\r\";}
    \"yes/no\" {send \"yes\r\"; exp_continue;}
      }
expect eof"

expect -c "
set timeout -1
spawn ssh ${FZIDT_DC_USER}@${FZIDT_DC_IP} \"docker ps -a | awk '{print $NF}' | grep ${DEPLOY_CONTAINER_NAME} && docker rm ${DEPLOY_CONTAINER_NAME}\"
expect {
    \"*assword\" {send \"${FZIDT_DC_PASS}\r\";}
    \"yes/no\" {send \"yes\r\"; exp_continue;}
      }
expect eof"

expect -c "
set timeout -1
spawn bash -c \"scp -r ${CI_PROJECT_DIR}/target/project-1.0.0-SNAPSHOT.jar ${FZIDT_DC_USER}@${FZIDT_DC_IP}:/projects/service/FzidtProMgtSv/data/project-1.0.0-SNAPSHOT.jar\"
expect {
    \"*assword\" {send \"${FZIDT_DC_PASS}\r\";}
    \"yes/no\" {send \"yes\r\"; exp_continue;}
      }
expect eof"

expect -c "
set timeout -1
spawn ssh ${FZIDT_DC_USER}@${FZIDT_DC_IP} \"docker run --name ${DEPLOY_CONTAINER_NAME} -v /projects/service/FzidtProMgtSv/data:/data -p 31100:31100 -p 10050:22 -d -d ubuntu18:jvm bash -c \\\"/usr/sbin/sshd; cd /data; java -Dspring.profiles.active=pro -jar /data/project-1.0.0-SNAPSHOT.jar\\\"\"
expect {
    \"*assword\" {send \"${FZIDT_DC_PASS}\r\";}
    \"yes/no\" {send \"yes\r\"; exp_continue;}
      }
expect eof"